package com.greenantmall.utils;

import java.io.File;
import java.io.FileOutputStream;

import com.greenantmall.R;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.socialization.Socialization;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;

public class ShareUtil {

	private static Context mContext;
	private static ShareUtil shareUtil;

	private static final String FILE_NAME = "/shareutil_test.jpg";
	private String testImage;
	private static OnekeyShare oks;

	private ShareUtil() {
		ShareSDK.initSDK(mContext);
		ShareSDK.registerService(Socialization.class);
		initImagePath();
		initOnekeyShare();
	}

	public static ShareUtil getInstance(Context context) {
		mContext = context;
		if (shareUtil == null) {
			shareUtil = new ShareUtil();
		}

		return shareUtil;
	}

	private void initImagePath() {
		try {
			String cachePath =mContext.getCacheDir().getAbsolutePath();
			testImage = cachePath + FILE_NAME;
			File file = new File(testImage);
			if (!file.exists()) {
				file.createNewFile();
				Bitmap pic = BitmapFactory.decodeResource(
						mContext.getResources(), R.drawable.pic);
				FileOutputStream fos = new FileOutputStream(file);
				pic.compress(CompressFormat.JPEG, 100, fos);
				fos.flush();
				fos.close();
			}
		} catch (Throwable t) {
			t.printStackTrace();
			testImage = null;
		}
	}

	// 此方法的代码从DemoPage中复制而来
	private void initOnekeyShare() {
		oks = new OnekeyShare();
		//oks.setAddress("12345678901");
		oks.setTitle(mContext.getString(R.string.share));
		oks.setTitleUrl("http://mob.com");
		oks.setText(mContext.getString(R.string.share_content));
		oks.setImagePath(testImage);
		oks.setImageUrl("http://img2.imgtn.bdimg.com/it/u=3717051771,590515419&fm=21&gp=0.jpg");
		oks.setUrl("http://www.mob.com");
		oks.setFilePath(testImage);
		oks.setComment(mContext.getString(R.string.share));
		oks.setSite(mContext.getString(R.string.app_name));
		oks.setSiteUrl("http://mob.com");
		//oks.setVenueName("ShareSDK");
		//oks.setVenueDescription("This is a beautiful place!");
		//oks.setLatitude(23.056081f);
		//oks.setLongitude(113.385708f);
		oks.disableSSOWhenAuthorize();
		oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
			public void onShare(Platform platform, ShareParams paramsToShare) {
				// 改写twitter分享内容中的text字段，否则会超长，
				// 因为twitter会将图片地址当作文本的一部分去计算长度
				if ("Twitter".equals(platform.getName())) {
					paramsToShare.setText(platform.getContext().getString(
							R.string.share_content_short));
				}
			}
		});
	}
	
	public void share(){
		
		oks.show(mContext);	
	}

}
